package com.bw.controller.order;


import com.alibaba.fastjson.JSON;
import com.bw.entity.dc.PiLinkman;
import com.bw.entity.order.PiOrderItem;
import com.bw.resp.ResponseResult;
import com.bw.service.order.IPiOrderItemService;
import com.bw.utils.ReadExcel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.order.PiOrderItem;
import com.bw.resp.ResponseResult;
import com.bw.service.order.IPiOrderItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *  订单零件
 * @author yzz
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/orderitem")
public class PiOrderItemController {
    @Autowired
    private IPiOrderItemService iPiOrderItemService;

    private final Logger logger = LoggerFactory.getLogger(PiOrderItemController.class);

    @PostMapping("/importExcel")
    public ResponseResult imporExcel(MultipartFile file) {
        logger.info("订单零件导入开始");
        ResponseResult re = new ResponseResult(false, 400, "上传失败");
        try {
            //获取后缀
            String hou = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'), file.getOriginalFilename().length());
            InputStream in = file.getInputStream();
            //调用封装工具类，处理数据
            List<Map> readExcel = ReadExcel.readExcel(hou, in);
            String jsonString = JSON.toJSONString(readExcel);//转成string
            System.out.println(jsonString);
            List<PiOrderItem> plist = JSON.parseArray(jsonString, PiOrderItem.class);//转成对象集合
            //保存
            re.setSuccess(true);
            re.setMessage("上传成功");
            re.setResult(plist);
            logger.info("订单联系人导入结束");
            return re;
        } catch (Exception e) {
            logger.error("订单联系人导入失败,{}", e.toString());
            return re;
        }
    }
    /**
     * 根据订单id查询对应的零件
     * @param id
     * @return
     */
    @PostMapping("getById/{id}")
    public ResponseResult getById(@PathVariable("id")Long id ){
        logger.info("订单零件回显查询开始");
        ResponseResult re=new ResponseResult(false,400,"查询失败");
        try {
            QueryWrapper qw = new QueryWrapper();
            if (id != null) {//对比传入ID和订单id相同的零件
                qw.eq("orderId",id);
            }
            List<PiOrderItem> list = iPiOrderItemService.list(qw);
            if(!list.isEmpty()){
                //PiOrderItem byId = list.get(0);
                re.setResult(list);
            }
            re.setCode(200);
            re.setMessage("查询成功");
            re.setSuccess(true);
            logger.info("订单零件回显查询结束");
            return  re;
        } catch (Exception e) {
            logger.error("订单零件回显查询错误,{}"+e.toString());
            return  re;
        }
    }

    /**
     * 根据订单code查询对应的零件列表
     * @return
     */
    @PostMapping("itemlist/{orderId}")
    public ResponseResult itemlist(@PathVariable("orderId") Long orderId ){
        logger.info("订单orderId查询对应的零件列表查询开始");
        ResponseResult re=new ResponseResult(false,400,"查询失败");
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("orderId",orderId);
            queryWrapper.orderByDesc("sn");
            List<PiOrderItem> list = iPiOrderItemService.list(queryWrapper);
            re.setResult(list);
            re.setMessage("查询成功");
            re.setSuccess(true);
            logger.info("订单code查询对应的零件列表结束");
            return  re;
        } catch (Exception e) {
            logger.error("订单code查询对应的零件列表错误,{}"+e.toString());
            logger.info("订单orderId查询对应的零件列表结束");
            return  re;
        }
    }
}
